<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>医生管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 搜索表单 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">医生姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="docName" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">科室名称</label>
                            <div class="layui-input-inline">
                                <select name="departmentId" id="departmentIdSelect">
                                    <option value="">请选择科室</option>
                                    <!-- 科室数据可通过接口加载， 剩下的值是从数据库里面查询出来 -->
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">职务名称</label>
                                <div class="layui-input-inline">
                                    <select name="positionId" id="positionIdSelect">
                                        <option value="">请选择职务</option>
                                        <!-- 职务数据可通过接口加载，此处为静态示例 -->
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <!-- 工具栏 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <!-- 表格容器 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 表格操作按钮模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        <!-- 编辑/添加弹窗 -->
        <div id="edit" style="display: none">
            <div class="layui-form layuimini-form" lay-filter="editForm">
                <div class="layui-form-item" id="docId-laylabel">
                    <label class="layui-form-label required">医生ID</label>
                    <div class="layui-input-inline">
                        <input type="text" name="docId" placeholder="请输入医生ID" readonly value="" class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="docName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">性别</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="docGender" value="男" title="男" checked>
                        <input type="radio" name="docGender" value="女" title="女">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">年龄</label>
                    <div class="layui-input-inline">
                        <input type="number" name="docAge" lay-verify="required" lay-reqtext="年龄不能为空" placeholder="请输入年龄" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="docPhone" lay-verify="required" lay-reqtext="电话不能为空" placeholder="请输入电话" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">科室</label>
                    <div class="layui-input-inline">
                        <select name="departmentId" id="editDepartmentIdSelect" lay-verify="required">
                            <option value="">请选择科室</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label ">级别</label>
                    <div class="layui-input-inline">
                        <select name="levelId" id="editLevelIdSelect" >
                            <option value="">请选择级别</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required" >职务</label>
                    <div class="layui-input-inline">
                        <select name="positionId" id="editPositionIdSelect" lay-verify="required">
                            <option value="">请选择职务</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 加载科室、职务下拉框（模拟数据，实际应从接口获取）
        function loadSelectOptions(url, selectId) {
            $.getJSON(url, function (res) {
                if (res.code === 0 && res.data  ) {
                    var options = '';
                    res.data.forEach(function (item) {
                        options += `<option value="${item.id}">${item.name}</option>`;
                    });
                    $(selectId).append(options);
                    form.render('select');
                }
            });
        }

        // 页面初始化时加载下拉框数据
        //查询所有的部门信息  id  name
        loadSelectOptions('/department/queryAll', '#departmentIdSelect');
        //查询所有的职务信息  id  name
        loadSelectOptions('/position/queryAll', '#positionIdSelect');
        //查询所有的职务信息  id  name
        loadSelectOptions('/position/queryAll', '#editPositionIdSelect');
        //查询所有的部门信息  id  name
        loadSelectOptions('/department/queryAll', '#editDepartmentIdSelect');
        //查询所有的级别信息  id  name
        loadSelectOptions('/level/queryAll', '#editLevelIdSelect');

        table.render({
            elem: '#currentTableId',
            url: '/doctor/queryPage', // 对接医生查询接口
            method: 'post',
            contentType: 'application/json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'docId', width: 100, title: 'ID', sort: true},
                {field: 'docName', width: 120, title: '姓名'},
                {field: 'docGender', width: 80, title: '性别'},
                {field: 'docAge', width: 80, title: '年龄', sort: true},
                {field: 'depName', width: 150, title: '科室'},
                {field: 'positionName', width: 150, title: '职务'},
                {field: 'docPhone', width: 150, title: '电话'},
                {field: 'docJoinTime', width: 160, title: '加入时间'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 50],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            response: {
                statusName: 'code',
                statusCode: 0,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            }
        });

        // 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: {
                    docName: data.field.docName,
                    departmentId: data.field.departmentId,
                    positionId: data.field.positionId
                }
            });
            return false;
        });

        // 工具栏事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                $("#docId-laylabel").hide();
                var index = layer.open({
                    title: '添加医生',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '60%'],
                    content: $("#edit")
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                var checkStatus = table.checkStatus('currentTableId');
                if (checkStatus.data.length === 0) {
                    layer.msg('请选择要删除的数据');
                    return;
                }
                var ids = checkStatus.data.map(item => item.docId);
                layer.confirm('确定删除选中的医生吗？', function () {
                    // 实际开发中调用删除接口
                    layer.msg('删除操作已执行');
                });
            }
        });

        // 行工具事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $("#docId-laylabel").show();
                form.val("editForm", data);
                var index = layer.open({
                    title: '编辑医生',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['40%', '60%'],
                    content: $("#edit")
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {
                $.getJSON("/doctor/delete?id=" + data.docId, function (res) {
                    if (res.code === 0) {
                        layer.msg("删除成功");
                        obj.del();
                    } else {
                        layer.msg("删除失败");
                    }
                });
            }
        });
    });
</script>
</body>
</html>
